package com.xncoding.pos.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xncoding.pos.common.model.AbstractPageQuery;
import com.xncoding.pos.common.model.ResultGenerator;
import com.xncoding.pos.common.model.ResultModel;
import com.xncoding.pos.dao.TbUserDao;
import com.xncoding.pos.feign.DynamicClient;
import com.xncoding.pos.feign.MessFeignService;
import com.xncoding.pos.model.LoginParam;
import com.xncoding.pos.model.TbUser;
import com.xncoding.pos.nacos.NacosValueConfig;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class TestController {

    @Resource
    NacosValueConfig nacosValueConfig;

    @Resource
    TbUserDao tbUserDao;

    @Resource
    MessFeignService messFeignService;

    @Resource
    DynamicClient dynamicClient;

    @GetMapping("/getPageData")
    public ResultModel getPageData(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize) {
        //分页查询，包括分页和总数查询，第三个参数是控制是否计算总数，默认是true,true为查询总数，分页效果只对其后的第一个查询有效。
        // PageHelper.startPage(pageNo, pageSize);

        //根据字段排序:age desc
        PageHelper.startPage(pageNo, pageSize, "age asc");

        //有分页
        List<TbUser> userList = tbUserDao.queryAll(null);

        //没分页,除非再次调用PageHelper.startPage
        userList = tbUserDao.queryAll(null);

        // ResultModel<Page> page = userService.getPage();
        return ResultGenerator.genSuccessResult(userList);
    }

    @ApiOperation("正常分页方法")
    @PostMapping(value = "pageInfoUser")
    @ApiOperationSupport(ignoreParameters = {"user.id", "user.age"})
    public PageInfo<TbUser> pageInfoUser(@Validated @RequestBody AbstractPageQuery pageQuerya) {
        // PageHelper.startPage(pageNum,pageNo);
        // PageInfo<TbUser> tbUserPageInfo = new PageInfo<>(tbUserDao.queryAll(null));
        TbUser tbUser = new TbUser();
        tbUser.setAge(22);
        //对应的lambda用法
        PageInfo tbUserPageInfo = PageHelper.startPage(pageQuerya.getPageNo(), pageQuerya.getPageSize()).doSelectPageInfo(() -> tbUserDao.queryAll(tbUser));

        return tbUserPageInfo;
    }

    @GetMapping(value = "pageUser")
    public Page<TbUser> pageUser(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        //这种方法,只包含分页之后的数据,没有其他字段
        Page<TbUser> userPage = tbUserDao.selectUserPage();
        return userPage;
    }


    @ApiOperation("feignTest1")
    @GetMapping("/feignTest_getByToken")
    public ResultModel feignTest(@RequestHeader(name = "token") String token) {
        return messFeignService.verification();
    }

    @ApiOperation("feignTest2")
    @GetMapping("/feignTest_getUser")
    public ResultModel getUser(@RequestParam String userId, @RequestParam Integer sleepSeconds) {
        return messFeignService.getUser(userId, sleepSeconds * 1000);
    }

    @ApiOperation("动态的feign测试")
    @GetMapping("/feignTest3")
    public void feignTest3(@RequestParam Integer flag) {
        LoginParam loginParam=new LoginParam();
        loginParam.setUsername("111");
        loginParam.setPassword("2222");
        String feignName = "resttemplate-server";

        if (flag==1) {
            feignName="resttemplate-server1";
        }
        Object result = dynamicClient.executePostApi(feignName, "/jwt/login", loginParam);
        log.info("==========>" + JSONObject.toJSONString(result));
    }


    @ApiOperation("getDataFromNacos")
    @GetMapping("/getDataFromNacos")
    public Map getDataFromNacos() {

        log.info("-----打印日志------{}", nacosValueConfig.getOrgCodes());
        log.info("-----打印日志------{}", nacosValueConfig.getOrgTag());
        return nacosValueConfig.getMaps();
    }


}
